<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>初始化一个 cURL 共享句柄。</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.curl-share-errno.html">« curl_share_errno</a></li>
      <li style="float: right;"><a href="function.curl-share-setopt.html">curl_share_setopt »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.curl.html">cURL 函数</a></li>
    <li>初始化一个 cURL 共享句柄。</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.curl-share-init" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">curl_share_init</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">curl_share_init</span> &mdash; <span class="dc-title">初始化一个 cURL 共享句柄。</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.curl-share-init-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>curl_share_init</strong></span>(): <span class="type">resource</span></div>

  <p class="para rdfs-comment">
   运行在 cURL 句柄之间共享数据。
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.curl-share-init-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.curl-share-init-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回类型为&quot;cURL 共享句柄&quot;的资源。
  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.curl-share-init-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4211">
    <p><strong>示例 #1 <span class="function"><strong>curl_share_init()</strong></span> 函数的范例：</strong></p>
    <div class="example-contents"><p>
     以下范例将会创建一个 cURL 共享句柄，并且往其中添加两个 cURL 句柄，最后用共享的 cookie 数据运行它们。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;创建&nbsp;cURL&nbsp;共享句柄，并设置共享&nbsp;cookie&nbsp;数据<br /></span><span style="color: #0000BB">$sh&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">curl_share_init</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">curl_share_setopt</span><span style="color: #007700">(</span><span style="color: #0000BB">$sh</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">CURLSHOPT_SHARE</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">CURL_LOCK_DATA_COOKIE</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;初始化第一个&nbsp;cURL&nbsp;句柄，并将它设置到共享句柄<br /></span><span style="color: #0000BB">$ch1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">curl_init</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://example.com/"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">curl_setopt</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">CURLOPT_SHARE</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sh</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;执行第一个&nbsp;cURL&nbsp;句柄<br /></span><span style="color: #0000BB">curl_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch1</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;初始化第二个&nbsp;cURL&nbsp;句柄，并将它设置到共享句柄<br /></span><span style="color: #0000BB">$ch2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">curl_init</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://php.net/"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">curl_setopt</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">CURLOPT_SHARE</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sh</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;执行第二个&nbsp;cURL&nbsp;句柄<br />//&nbsp;&nbsp;all&nbsp;cookies&nbsp;from&nbsp;$ch1&nbsp;handle&nbsp;are&nbsp;shared&nbsp;with&nbsp;$ch2&nbsp;handle<br /></span><span style="color: #0000BB">curl_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch2</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;关闭&nbsp;cURL&nbsp;共享句柄<br /></span><span style="color: #0000BB">curl_share_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$sh</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;关闭&nbsp;cURL&nbsp;共享句柄<br /></span><span style="color: #0000BB">curl_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">curl_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.curl-share-init-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.curl-share-setopt.html" class="function" rel="rdfs-seeAlso">curl_share_setopt()</a> - 为 cURL 共享句柄设置选项。</span></li>
    <li class="member"><span class="function"><a href="function.curl-share-close.html" class="function" rel="rdfs-seeAlso">curl_share_close()</a> - 关闭 cURL 共享句柄</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>